package com.digital.herbarium.Util;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.digital.herbarium.Entity.User;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.Mapper.UserMapper;


public class UserUtil {

    public  final  static  Integer[] USERSTATE = {-1,0,1};

    public static void checkUser(User user,boolean checkpwd){

        if(StrUtil.hasEmpty(user.getUsername()) || StrUtil.hasBlank(user.getUsername())){
            throw new CustomException(400,"人员姓名不能为空");
        }

        if(StrUtil.hasEmpty(user.getUseraccount()) || StrUtil.hasBlank(user.getUseraccount())){
            throw new CustomException(400,"登录账户不能为空");
        }

        if(checkpwd && (StrUtil.hasEmpty(user.getPassword()) || StrUtil.hasBlank(user.getPassword()))){
            throw new CustomException(400,"密码不能为空");
        }

        if(StrUtil.hasEmpty(user.getTelephone()) || StrUtil.hasBlank(user.getTelephone())){
            throw new CustomException(400,"联系方式不能为空");
        }

        if(!Validator.isMobile(user.getTelephone())){
            throw new CustomException(400,"联系方式不合法");
        }

        if(StrUtil.hasEmpty(user.getDepartment()) || StrUtil.hasBlank(user.getDepartment())){
            throw new CustomException(400,"部门名称不能为空");
        }

    }

    public  static void checkState(Integer state){
         if(!ArrayUtil.contains(USERSTATE,state)){
             throw new CustomException(400,"状态不合法,应为-1(删除),1(正常),0(停用)");
         }
    }

    public  static void checkInfoExist(User user, UserMapper userMapper){
        //判断登录账户是否已存在
        User u = new User();
        u.setUseraccount(user.getUseraccount());
        int c1 = userMapper.checkUserInfo(u);
        if(c1 > 0){
            throw  new CustomException(400,"该账户已存在，请重新设置");
        }
        //判断手机号是否存在
        User u1 = new User();
        u1.setTelephone(user.getTelephone());
        int c2 = userMapper.checkUserInfo(u1);
        if(c2 > 0){
            throw  new CustomException(400,"该手机号已存在，请重新设置");
        }
    }
    //

    public  static void checkEditUserInfo(User user, UserMapper userMapper){
        //判断登录账户是否已存在
        User u = new User();
        u.setUseraccount(user.getUseraccount());
        u.setId(user.getId());
        int c1 = userMapper.checkEditUserInfo(u);
        if(c1 > 0){
            throw  new CustomException(400,"该账户已存在，请重新设置");
        }
        //判断手机号是否存在
        User u1 = new User();
        u1.setTelephone(user.getTelephone());
        u1.setId(user.getId());
        int c2 = userMapper.checkEditUserInfo(u1);
        if(c2 > 0){
            throw  new CustomException(400,"该手机号已存在，请重新设置");
        }
    }

}
